package a15_MidStudentSystem;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author quan
 * @Description 升级版学生管理系统
 * @Date 2023/2/1 11:27
 */
public class TestDemo {
    public static void main(String[] args) {

        //定义一个存储用户信息的数组，<>里要写UserInfo类，而不是String
        ArrayList<UserInfo> list = new ArrayList<>();
        //登录界面
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作：1登录 2注册 3忘记密码");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择：");
            String choose = sc.next();
            switch (choose) {
                case "1":
                    login(list);
                    break;
                case "2":
                    register(list);
                    break;
                case "3":
                    forgetPassword(list);
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }


    }

    //三次机会的方法
    //*********************不对**************
    public static boolean threeChoose(ArrayList<UserInfo> list){
        for (int i = 0; i < 3; i++) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入登录的用户名：");
            String reLoginName = sc.next();
            System.out.println("请重新输入登录的密码：");
            String reLoginPassword = sc.next();
            boolean logFlag = checkLoginNamePassword(reLoginName,reLoginPassword,list);
            if(logFlag){
                return true;
            }
        }
        return false;
    }
    //方法:获取一个验证码
    public static String getLoginVerifyCode(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请自行输入一个验证码：");
        return (sc.next());
    }

    public static boolean checkLoginVerifyCode(String loginVerifyCode,String rightVerifyCode){
        boolean logFlag;
        if(loginVerifyCode.equals(rightVerifyCode)){
            logFlag = true;
        }else{
            logFlag = false;
        }
        return logFlag;
    }
    public static boolean checkLoginName(String loginName,ArrayList<UserInfo> list){
        boolean logFlag = false;
        for (int i = 0; i < list.size(); i++) {
            UserInfo ui = list.get(i);//获取用户的四个属性信息
            if(loginName.equals(ui.getUsername())){
                logFlag = true;
            }
            else{
                logFlag = false;
            }
        }
        return logFlag;
    }

    public static void reLoginVerifyCode(String rightVerifyCode){
        //**************while循环*******break********变量不用定义
        boolean logFlag = false;
        Scanner sc = new Scanner(System.in);
        System.out.println("请重新输入验证码：");
        String LoginVerifyCode = sc.next();
        logFlag = checkLoginVerifyCode(LoginVerifyCode,rightVerifyCode);
    }

    public static boolean checkLoginNamePassword(String loginName, String loginPassword, ArrayList<UserInfo> list){

        for (int i = 0; i < list.size(); i++) {
            if(loginName.equals(list.get(i).getUsername()) && loginPassword.equals(list.get(i).getPassword())){
                return true;
            }
        }
        return false;
    }

    public static String readKeyboard(){
        Scanner sc = new Scanner(System.in);
        return sc.next();
    }
    //登录
    public static void login(ArrayList<UserInfo> list_) {
        boolean logFlag;

        String rightVerifyCode = getLoginVerifyCode();
        System.out.println("您的验证码是："+rightVerifyCode);
        //键盘获取登录信息——>获取一个验证码——>安全校验

        System.out.println("请输入登录的用户名：");
        String loginName = readKeyboard();
        //判断用户名是否正确
        //****************数字和字母**********
        logFlag = checkLoginName(loginName,list_);
//        System.out.println(logFlag);
        if(logFlag){
            System.out.println("请输入登录的验证码：");
            String loginVerifyCode = readKeyboard();
            //再次判断验证码是否正确
            logFlag = checkLoginVerifyCode(loginVerifyCode,rightVerifyCode);
            if(logFlag){
//                System.out.println("请输入登录的密码：");
//                String loginPassword = readKeyboard();
                //判断用户名和密码的方法:如果正确
                logFlag = threeChoose(list_);
                if(logFlag){
                    System.out.println("登录成功");
                }else {
                    System.out.println("三次机会登录失败");
                }
            }else{//如果验证码不对，再重新输入
               //调用重新输入验证码的方法
                reLoginVerifyCode(rightVerifyCode);
            }
        }
        else {
            System.out.println("没有该用户，请先注册！");
        }
    }

    //注册
    public static void register(ArrayList<UserInfo> list) {
        //键盘录入信息——>往集合里存储四个属性信息——>并分别校验安全性
        //键盘获取信息
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入注册的用户名：");
        String regName = sc.next();
        //定义一个布尔类型的标志，用于接收注册方法的校验安全性（4个检查方法）的返回值类型
        boolean flag;
        //检查用户名信息
        flag = checkRegId(regName);
        if(flag){
            System.out.println("请输入注册密码：");
            String regPassword1 = sc.next();
            System.out.println("请确认注册密码：");
            String regPassword2 = sc.next();
            flag = printPasswordTwice(regPassword1,regPassword2);
            if(flag){
                //检查身份证信息
                System.out.println("请输入注册身份证号：");
                String regIdCard = sc.next();
                flag = checkRegIdCard(regIdCard);
                if(flag){
                    //检查手机号信息
                    System.out.println("请输入注册手机：");
                    String regTel = sc.next();
                    flag = checkRegTel(regTel);
                    if(flag){
                        //创建一个用户对象
                        UserInfo ui = new UserInfo(regName,regPassword1,regIdCard,regTel);
                        list.add(ui);//将对象内容添加到集合中
                        System.out.println("注册成功！");
                    }
                }
            }
        }else{
            System.out.println("用户名格式不对");
        }
    }

    //检查注册的手机号
    public static boolean checkRegTel(String regTel){
        boolean flag = false;
        if(regTel.length()==11 && regTel.charAt(0) !=48 ){
            for (int i = 0; i < regTel.length(); i++) {
                if(regTel.charAt(i)>=48 && regTel.charAt(i)<=57){
                    flag = true;
                }
            }
        }
        return flag;
    }
    //检查注册的身份证号
    public static boolean checkRegIdCard(String regIdCard){
        boolean flag = false;
        if(regIdCard.length()==18 && regIdCard.charAt(0)!=48){
            for (int i = 0; i < regIdCard.length(); i++) {
                if(i<regIdCard.length()-1 && (regIdCard.charAt(i)>48 && regIdCard.charAt(i)<57)){
                    flag = true;
                }else if((regIdCard.charAt(i)>48 && regIdCard.charAt(i)<57) || (regIdCard.charAt(i)==88) || (regIdCard.charAt(i)==120)){
                    flag = true;
                }else {
                    flag = false;
                }
            }
        }
        return flag;
    }
    //注册输入两次密码
    public static boolean printPasswordTwice(String regPassword1,String regPassword2){
        if(regPassword1.equals(regPassword2)){
            return true;
        }else{
            return false;
        }
    }
    //注册的用户名检查
    public static boolean checkRegId(String regName){
        if((regName.length()>3) && (regName.length()<15)){
            int count = 0;//定义字母的个数
            for (int i = 0; i < regName.length(); i++) {//判断是否是字母或数字
                if((regName.charAt(i) >=48 && regName.charAt(i)<=57 )||(regName.charAt(i) >=65 && regName.charAt(i)<=90 ) ||(regName.charAt(i) >=88 && regName.charAt(i)<=120)){
                    if((regName.charAt(i) >=65 && regName.charAt(i)<=90 ) ||(regName.charAt(i) >=88 && regName.charAt(i)<=120)){
                        count++;
                    }
                }else {
                    return false;
                }
            }
            if(count != 0){
                return true;
            }else {
                return false;
            }
        }else{
            return false;
        }
    }



    //检查身份证和手机号是否一致的方法
    public static boolean checkuserTel(String userName,String userTel,ArrayList<UserInfo> list){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(userName)){
                if(list.get(i).getPhoneNumber().equals(userTel)){
                    return true;
                }
            }
        }
        return false;
    }
    public static boolean checkIdCard(String userName,String userIdCard,ArrayList<UserInfo> list){

        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(userName)){
                if(list.get(i).getIdCard().equals(userIdCard)){
                    return true;
                }
            }
        }
        return false;
    }
    //忘记密码
    public static void forgetPassword(ArrayList<UserInfo> list) {
        boolean fgFalg;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的用户名：");
        String userName = sc.next();
        fgFalg = checkLoginName(userName,list);
        if(fgFalg){
            System.out.println("请输入您的身份证号：");
            String userIdCard = sc.next();
            System.out.println("请输入您的手机号：");
            String userTel = sc.next();
            boolean fgFalg1 = checkIdCard(userName,userIdCard,list);
            boolean fgFalg2 = checkuserTel(userName,userTel,list);
            if(fgFalg1 && fgFalg2){
                System.out.println("请修改您的密码：");
                String newPassword = sc.next();

                for (int i = 0; i < list.size(); i++) {
                    if(list.get(i).getPhoneNumber().equals(userTel)){
                        list.get(i).setPassword(newPassword);
                        System.out.println("密码修改成功");
                    }
                }
            }
        }else{
            System.out.println("用户不存在，请先注册");
        }
    }
}
